﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Microsoft.Practices.Unity;
using Wicresoft.UnifyShow.Business;
using Wicresoft.UnifyShow2.Portal.Models;

namespace Wicresoft.UnifyShow2.Portal.Controllers
{
    [Authorize]
    public class AdminBDController : BaseController
    {
        private IUser_X user;
        private ISiteBranch siteBranch;
        private ISelfBreakDown selfBreakdown;

        protected override void Dispose(bool disposing)
        {
            ((IDisposable)user).Dispose();
            ((IDisposable)siteBranch).Dispose();
            ((IDisposable)selfBreakdown).Dispose();
        }

        [InjectionConstructor]
        public AdminBDController(IUser_X user, ISiteBranch siteBranch, ISelfBreakDown selfBreakdown)
        {
            this.user = user;
            this.siteBranch = siteBranch;
            this.selfBreakdown = selfBreakdown;
        }

        //
        // GET: /AdminEvent/

        public ActionResult Add()
        {
            base.SetRoles(user);
            ViewBag.IsConfigAdmin = IsConfigAdmin;
            var USER_X = user.GetUser_XByName(User.Identity.Name);
            if (USER_X == null)
                throw new Exception("USER_X is null.");
            ViewBag.UserDept = USER_X.Department;
            ViewBag.UserCompany = USER_X.Company;
            if (!IsConfigAdmin) return new HttpStatusCodeResult(403);

            ViewData["Categories"] = selfBreakdown.GetCategory().
                Select(o => new SelectListItem()
                {
                    Text = o.LvlName,
                    Value = o.ParentID.ToString(),
                    Selected = false
                });

            return View();
        }

        [HttpPost]
        public ActionResult Add(SelfBreakDwonModel model)
        {

            base.SetRoles(user);
            ViewBag.IsConfigAdmin = IsConfigAdmin;
            var USER_X = user.GetUser_XByName(User.Identity.Name);
            if (USER_X == null)
                throw new Exception("USER_X is null.");
            ViewBag.UserDept = USER_X.Department;
            ViewBag.UserCompany = USER_X.Company;

            List<Dictionary<uint, string>> syncs = new List<Dictionary<uint, string>>();
            Dictionary<uint, string> sync = new Dictionary<uint, string>();

            int listExsit = selfBreakdown.GetExistBreakDwon(model.LvlName).Count();

            int category = Convert.ToInt32(Request.Form["Categroy"]);
            model.ParentID = category;

            if (listExsit < 1)
            {
                //Add SelfService
                selfBreakdown.Add(model);

                //Add ITSM
                //sync[536870917] = model.PartialName;   //事件性质
                //sync[536870914] = model.Applvl1;
                //sync[536870915] = model.Applvl2;
                //sync[536870916] = model.Applvl3;
                syncs.Add(sync);
                ARHelper.SyncAddListEventTypeToAR(syncs);
                ViewBag.Post = true;
            }
            else
            {
                ViewBag.error = "该故障申报服务已存在！";
            }
            return View(model);
        }

        public ActionResult Edit(int id)
        {
            base.SetRoles(user);
            ViewBag.IsConfigAdmin = IsConfigAdmin;
            SelfBreakDwonModel model = selfBreakdown.GetSelfBreakDwonByID(id);
            return PartialView(model);
        }

        [HttpPost]
        public void Edit(SelfBreakDwonModel model)
        {

            List<Dictionary<uint, string>> syncs = new List<Dictionary<uint, string>>();
            Dictionary<uint, string> syncdelete = new Dictionary<uint, string>();
            //syncdelete[536870917] = model.PartialName;   //事件性质
            //syncdelete[536870914] = model.Applvl1;
            //syncdelete[536870915] = model.Applvl2;
            //syncdelete[536870916] = model.Applvl3;

            ARHelper.SyncDeleteBusinessSystemToAR(syncdelete);

            Dictionary<uint, string> sync = new Dictionary<uint, string>();
            //sync[536870917] = model.PartialName;   //事件性质
            //sync[536870914] = model.Applvl1;
            //sync[536870915] = model.Applvl2;
            //sync[536870916] = model.Applvl3;
            syncs.Add(sync);

            ARHelper.SyncAddListBusinessSystemToAR(syncs);

            selfBreakdown.Update(model);
        }

        [HttpPost]
        public void Delete(int id)
        {
            SelfBreakDwonModel model = selfBreakdown.GetSelfBreakDwonByID(id);
            selfBreakdown.Delete(id);
            //delete ITSM
            Dictionary<uint, string> sync = new Dictionary<uint, string>();
            //sync[536870917] = model.PartialName;   //事件性质
            //sync[536870914] = model.Applvl1;
            //sync[536870915] = model.Applvl2;
            //sync[536870916] = model.Applvl3;

            ARHelper.SyncDeleteBusinessSystemToAR(sync);
            SyncHelper.SyncDeleteBusinessSystemToSelf(sync);
        }

        [HttpPost]
        public void Publish(int id)
        {
            //do something about the publish
        }

    }
}
